<template>
  <div @mouseover="isOver = true" @mouseleave="isOver = false">
    <div class="ChatItem">
      <div class="ChatAvatar">
        <n-image v-if="value.role === 'user' && chatSettings['base'].template['avatar']?.value[0]?.url"
          :src="chatSettings['base'].template['avatar']?.value[0]?.url" height="40" style="border-radius: 8px;" />
        <n-avatar v-else-if="value.role === 'system'" round size="large">
          <n-icon>
            <SettingsSharp />
          </n-icon>
        </n-avatar>
        <n-image v-else-if="value.role === 'assistant' && chatMap.get(currentChat)?.user?.avatar"
          :src="chatMap.get(currentChat)?.user?.avatar" height="40" style="border-radius: 8px;" />
        <n-avatar v-else-if="value.role === 'assistant' && !chatMap.get(currentChat)?.user" round
          size="large">AI</n-avatar>
        <n-avatar size="large" v-else round>默认</n-avatar>
      </div>
      <div>
        <div class="ChatRole">
          {{ value.role === 'system' ? 'system' : value.role === 'assistant' ? chatMap.get(currentChat)?.user?.name ??
            'AI' :
            chatSettings['base'].template['username'].value ?? '你' }}
        </div>
        <div class="ChatMessage" :class="{ generateError: value.status === '已失败' }">
          <ChatMessageContent v-model:value="value.content" />
        </div>
      </div>
    </div>
    <div style="height: 40px;">
      <n-dialog-provider>
        <ChatItemAction :index="index" :value="value" v-if="isOver" />
      </n-dialog-provider>
    </div>
  </div>
</template>

<script setup lang='ts'>
import { SettingsSharp } from '@vicons/material'
const { value, index } = defineProps<{
  value: Message,
  index: Number
}>()
const { chatSettings } = storeToRefs(chatSettingStore())
const { chatMap, currentChat } = storeToRefs(chatStore())
const isOver = ref(false)
</script>
<style lang="scss">
@media screen and (max-width: 800px) {
  .ChatItem {
    flex-direction: column;
  }
}

.ChatItem {
  min-height: 52px;
  width: 100%;
  position: relative;
  display: flex;

  .ChatAvatar {
    margin-right: 15px;
  }

  .ChatRole {
    font-weight: 600;
    font-family: 'ali-600';
  }

  .ChatMessage {
    white-space: pre-wrap;
  }
}

.generateError {
  color: #f5222d !important;
}

.n-dialog {
  width: auto !important;
}
</style>
